package com.example.gamecenter.fragments.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.gamecenter.R
import com.example.gamecenter.activitys.ListActivity
import com.example.gamecenter.adapter.HomeRecommendCardItem
import com.example.gamecenter.adapter.ListHomeRecommendCardAdapter
import com.example.gamecenter.adapter.RecommendItem
import com.example.gamecenter.utils.Utility
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator

class HomeRecommendFragment: Fragment(R.layout.home_recommend_fragment) {
    private lateinit var fragmentContext: Context;

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState);
        fragmentContext = (context ?: this) as Context;
        /**
         * banner列表
         */
        val bannerDataList = listOf(
            HomeRecommendFragment.BannerDataBean("https://img.zcool.cn/community/01f6db5bdbc1b6a801209252a5087c.jpg@2o.jpg"),
            HomeRecommendFragment.BannerDataBean("https://p5.itc.cn/images01/20210528/3ebb2f5458784d628f0152455ca8cdaa.jpeg"),
            HomeRecommendFragment.BannerDataBean("https://img.zcool.cn/community/01f0fe5a55f2b8a80120121f9db2cb.jpg@2o.jpg"),
        )
        val homeCardListView = view.findViewById<ListView>(R.id.home_card_list);
        val mLI = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater;
        homeCardListView.addHeaderView(mLI.inflate(R.layout.home_banner, null));
        val banner: Banner<HomeRecommendFragment.BannerDataBean, BannerImageAdapter<HomeRecommendFragment.BannerDataBean>> =
            view.findViewById(R.id.bannerView);
        banner.setAdapter(object : BannerImageAdapter<HomeRecommendFragment.BannerDataBean>(bannerDataList) {
            override fun onBindView(
                holder: BannerImageHolder,
                data: HomeRecommendFragment.BannerDataBean,
                position: Int,
                size: Int
            ) {
                //图片加载自己实现
                Glide.with(holder.itemView)
                    .load(data.imageUrl)
                    .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(30)))
                    .into(holder.imageView)
            }
        }).addBannerLifecycleObserver(this).setIndicator(CircleIndicator(fragmentContext))
        // 计算listview的高度并且撑开，不然会收缩成一个，需要先建立MyListView
//         Utility.setListViewHeightBasedOnChildren(homeCardListView);

        val listHomeRecommendCardAdapter = ListHomeRecommendCardAdapter(fragmentContext, false)
        homeCardListView.adapter = listHomeRecommendCardAdapter
        // 数据列表
        val recommendData = listOf(
            HomeRecommendCardItem("分类1",1, true,
                listOf(
                    RecommendItem("标题1", "https://img.sxsme.com.cn/uploadimg/ico/2023/1007/1696665441196246.jpg",1, "天剑传说"),
                    RecommendItem("标题2", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552459611151.jpg",2, "天剑传说"),
                    RecommendItem("标题3", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552498848724.jpg",3, "天剑传说"),
                    RecommendItem("标题4", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552534375775.png",4, "天剑传说"),
                    RecommendItem("标题5", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552566865753.png",5, "天剑传说"),
                    RecommendItem("标题6", "https://img.sxsme.com.cn/uploadimg/ico/2022/1214/1670986386653144.jpg",6, "天剑传说"),
                )
            ),
            HomeRecommendCardItem("分类2",1, false,
            listOf(
                RecommendItem("标题1", "https://img.sxsme.com.cn/uploadimg/ico/2023/1007/1696665441196246.jpg",1, "天剑传说"),
                RecommendItem("标题2", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552459611151.jpg",2, "天剑传说"),
                RecommendItem("标题3", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552498848724.jpg",3, "天剑传说"),
                RecommendItem("标题4", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552534375775.png",4, "天剑传说"),
                RecommendItem("标题5", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552566865753.png",5, "天剑传说"),
                RecommendItem("标题6", "https://img.sxsme.com.cn/uploadimg/ico/2022/1214/1670986386653144.jpg",6, "天剑传说"),
            )
            )
        )
        listHomeRecommendCardAdapter.setOnRecommendItemClickListener(object : ListHomeRecommendCardAdapter.OnRecommendItemClickListener {
            override fun onRecommendItemMoreClick(v: View?, p: HomeRecommendCardItem?) {
                // 处理点击事件
                if (v?.id == R.id.view_more) { // 查看更多
                    // Log.e("nanke", v?.id.toString())
                    val intent = Intent(fragmentContext, ListActivity().javaClass)
                    intent.putExtra("typeName", "${p?.typeName}")
                    intent.putExtra("typeId", p?.typeId)
                    startActivity(intent)
                }

            }
        })

        listHomeRecommendCardAdapter.setDatas(recommendData);
//        apiGetGameList(listHomeCardAdapter);

    }
    data class BannerDataBean(
        val imageUrl :String
    )
}